﻿using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Xml.Linq;
using Ionic.Zip;

namespace ZenBridge
{
    /// <summary>
    /// Helper functions to integrate ZenBridge with client applications. Currently only
    /// supports Google Picasa.
    /// </summary>
    internal static class ZenBridgeIntegrate
    {
        /// <summary>
        /// The GUID to idenitfy the Picasa button. This GUID must not change
        /// in order for Picasa to properly handle versioning.
        /// </summary>
        private const string PBFGUID = "f6fc6131-fec1-404f-b072-bd71dc22652e";

        /// <summary>
        /// Uses LINQ to XML API to generate the XML Picasa Button File.
        /// </summary>
        /// <returns>The XML PBF as an XDocument object.</returns>
        private static XDocument GeneratePicasaButtonXML()
        {
            XAttribute exe_nameAttr = new XAttribute("name", "exe_name");
            XAttribute exe_valueAttr = new XAttribute("value", Process.GetCurrentProcess().MainModule.FileName);
            XElement exe_paramElement = new XElement("param");
            exe_paramElement.Add(exe_nameAttr);
            exe_paramElement.Add(exe_valueAttr);

            XAttribute export_nameAttr = new XAttribute("name", "export");
            XAttribute export_valueAttr = new XAttribute("value", "1");
            XElement export_paramElement = new XElement("param");
            export_paramElement.Add(export_nameAttr);
            export_paramElement.Add(export_valueAttr);

            XAttribute message_nameAttr = new XAttribute("name", "export_message");
            XAttribute message_valueAttr = new XAttribute("value", "Sending to ZenBridge...");
            XElement message_paramElement = new XElement("param");
            message_paramElement.Add(message_nameAttr);
            message_paramElement.Add(message_valueAttr);

            XAttribute foreach_nameAttr = new XAttribute("name", "foreach");
            XAttribute foreach_valueAttr = new XAttribute("value", "1");
            XElement foreach_paramElement = new XElement("param");
            foreach_paramElement.Add(foreach_nameAttr);
            foreach_paramElement.Add(foreach_valueAttr);

            XAttribute verbAttr = new XAttribute("verb", "trayexec");
            XElement actionElement = new XElement("action");
            actionElement.Add(verbAttr);
            actionElement.Add(exe_paramElement);
            actionElement.Add(export_paramElement);
            actionElement.Add(message_paramElement);
            actionElement.Add(foreach_paramElement);

            XElement toolElement = new XElement("tooltip", "Upload to Zenfolio, using ZenBridge.");
            XElement labelElement = new XElement("label", "ZenBridge");

            XAttribute typeAttr = new XAttribute("type", "dynamic");
            XAttribute idAttr = new XAttribute("id", "archers-rally/{" + PBFGUID + "}");
            XElement buttonElement = new XElement("button");
            buttonElement.Add(typeAttr);
            buttonElement.Add(idAttr);
            buttonElement.Add(labelElement);
            buttonElement.Add(toolElement);
            buttonElement.Add(actionElement);

            XAttribute formatAttr = new XAttribute("format", "1");
            XAttribute versionAttr = new XAttribute("version", "1");
            XElement rootElement = new XElement("buttons");
            rootElement.Add(formatAttr);
            rootElement.Add(versionAttr);
            rootElement.Add(buttonElement);            
            
            XDocument buttonsDoc = new XDocument();
            buttonsDoc.Add(rootElement);

            XDeclaration declaration = new XDeclaration("1.0", "utf-8", "yes");
            buttonsDoc.Declaration = declaration;

            return buttonsDoc;

        }
        
        /// <summary>
        /// Installs the Picasa Button to integrate Picasa with ZenBridge.
        /// </summary>
        internal static void InstallPicasaButton()
        {
            XDocument button = GeneratePicasaButtonXML();

            using (ZipFile zip = new ZipFile())
            {
                zip.AddEntry("{" + PBFGUID + "}.pbf", button.ToString(), Encoding.UTF8);
                zip.Save(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), 
                    "ZenBridgeButton.pbz")
                    );
            }

            FileInfo buttonFile = new FileInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
                    "ZenBridgeButton.pbz")
                    );

            Process.Start("picasa://importbutton/?url=file:///" + buttonFile.FullName);
        }
    }
}
